package com.ly.test.controller;

import com.ly.common.core.bean.R;
import com.ly.system.test.entity.RedisInfo;
import com.ly.test.service.IRedisService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * redis监控
 *
 * @author jeecg
 */
@Slf4j
@RestController
@RequestMapping("/sys/actuator/redis")
@RequiredArgsConstructor
public class ActuatorRedisController {

    private final IRedisService redisService;

    /**
     * Redis详细信息
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/info")
    public R<?> getRedisInfo() {
        List<RedisInfo> infoList = redisService.getRedisInfo();
        return R.ok(infoList);
    }

    @GetMapping("/memoryInfo")
    public Map<String, Object> getMemoryInfo() {
        return redisService.getMemoryInfo();
    }

    /**
     * Redis历史性能指标查询(过去一小时)
     *
     * @return
     * @throws Exception
     * @author jeecg
     * @date 2024/5/14 14:56
     */
    @GetMapping("/metrics/history")
    public R<?> getMetricsHistory() {
        Map<String, List<Map<String, Object>>> metricsHistory = redisService.getMetricsHistory();
        return R.ok(metricsHistory);
    }
    // update-end---author:chenrui ---date:20240514  for：[QQYUN-9247]系统监控功能优化------------

    @GetMapping("/keysSize")
    public Map<String, Object> getKeysSize() {
        return redisService.getKeysSize();
    }

    /**
     * 获取磁盘信息
     */
    @GetMapping("/queryDiskInfo")
    public R<List<Map<String, Object>>> queryDiskInfo() {
        // 当前文件系统类
        FileSystemView fsv = FileSystemView.getFileSystemView();
        // 列出所有windows 磁盘
        File[] fs = File.listRoots();
        log.info("查询磁盘信息:" + fs.length + "个");
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < fs.length; i++) {
            if (fs[i].getTotalSpace() == 0) {
                continue;
            }
            Map<String, Object> map = new HashMap(5);
            map.put("name", fsv.getSystemDisplayName(fs[i]));
            map.put("max", fs[i].getTotalSpace());
            map.put("rest", fs[i].getFreeSpace());
            map.put("restPPT", (fs[i].getTotalSpace() - fs[i].getFreeSpace()) * 100 / fs[i].getTotalSpace());
            list.add(map);
            log.info(map.toString());
        }
        return R.ok(list);
    }
}
